package com.controller;

import com.constants.Constants;
import com.entity.AllTask;
import com.tools.BaseUntil;
import com.tools.TaskListener;
import com.until.CreatePage;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class QueryTaskController extends BaseUntil implements Initializable {
    public QueryTaskController() {
        //初始化时保存当前Controller实例
        Constants.controllers.put(this.getClass().getSimpleName(), this);
    }

    @FXML
    private Label queryTask;

    @FXML
    private TableView Alltview;

    @FXML
    private TableColumn id;
    @FXML
    private TableColumn title;
    @FXML
    private TableColumn info;
    @FXML
    private TableColumn tasktype;
    @FXML
    private TableColumn countpotato;
    @FXML
    private TableColumn countsleep;
    @FXML
    private TableColumn nowtype;
    @FXML
    private TableColumn msg;
    @FXML
    private TableColumn maketime;
    @FXML
    private TableColumn finishtime;

    public void clossWindow() {
        Stage stage = (Stage) queryTask.getScene().getWindow();
        stage.close();
    }


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        showList();
    }

    public void showList() {
        //TaskListener taskListener = new TaskListener();
        //String filePath = MainController.class.getClassLoader().getResource(Constants.fileName).getFile();
        //EasyExcel.read(Constants.filePath, AllTask.class, taskListener).sheet().doRead();
        ObservableList<AllTask> allTasks = FXCollections.observableArrayList();
        for (AllTask tasks : TaskListener.getAlltask()) {
            allTasks.add(tasks);
        }
        id.setCellValueFactory(new PropertyValueFactory("id"));//映射
        title.setCellValueFactory(new PropertyValueFactory("title"));
        info.setCellValueFactory(new PropertyValueFactory("info"));//映射
        tasktype.setCellValueFactory(new PropertyValueFactory("tasktype"));
        countpotato.setCellValueFactory(new PropertyValueFactory("countpotato"));
        countsleep.setCellValueFactory(new PropertyValueFactory("countsleep"));
        nowtype.setCellValueFactory(new PropertyValueFactory("nowtype"));
        msg.setCellValueFactory(new PropertyValueFactory("msg"));
        maketime.setCellValueFactory(new PropertyValueFactory("maketime"));
        finishtime.setCellValueFactory(new PropertyValueFactory("finishtime"));
        Alltview.getItems().setAll(allTasks);
    }

    public void ClickTwo(MouseEvent event) throws IOException {
        if (event.isPrimaryButtonDown() && event.getClickCount() == 2) {
            Node node = ((Node) event.getTarget()).getParent();
            TableRow row;
            if (node instanceof TableRow) {
                row = (TableRow) node;
            } else if (node.getParent() instanceof javafx.scene.layout.AnchorPane) {
                row = null;
            } else if (node.getParent() instanceof javafx.scene.layout.StackPane) {
                row = null;
            } else {
                // clicking on text part
                row = (TableRow) node.getParent();
            }
            if (!checkObjFieldIsNotNull(row)) {
                message("请点选数据");
                return;
            }
            if (!checkObjFieldIsNotNull(row.getItem())) {
                message("请点选数据");
                return;
            }
            AllTask item =(AllTask) row.getItem();
            if (!checkObjFieldIsNotNull(item)) {
                message("请点选数据");
                return;
            }
            if (!checkObjFieldIsNotNull(item.getId())) {
                message("请点选数据");
                return;
            }
            MainController.ditileTask = item;
            CreatePage.getNew("taskView.fxml");
        }
    }
}
